//
//  selfdefineCalendarView.m
//  CalendarView
//
//  Created by 曾墨 on 16/8/23.
//  Copyright © 2016年 YuanJiang. All rights reserved.
//

#import "selfdefineCalendarView.h"

static CGFloat ScreenWidth =0;
static CGFloat ScreenHight =0;

//类目
@interface selfdefineCalendarView ()

@property(nonatomic,strong) UIView *contentView;

@property(nonatomic,strong) UIDatePicker *datePicker;

@property(nonatomic,strong) UIButton *okBtn;

@property(nonatomic,strong) UIButton *cancelBtn;

@property(nonatomic,strong) NSDateFormatter *formatter;

@end

@implementation selfdefineCalendarView
//View最先走的方法
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        ScreenHight = [UIScreen mainScreen].bounds.size.height;
        ScreenWidth = [UIScreen mainScreen].bounds.size.width;
        [self setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.5]];
        //手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickEmpty:)];
        [self addGestureRecognizer:tap];
        [self makeUI];
    }
    return self;
}

-(void)makeUI
{
    _contentView = [[UIView alloc] initWithFrame:CGRectMake(10, (ScreenHight -290)/2, ScreenWidth-20, 290)];
    _contentView.backgroundColor = [UIColor whiteColor];
    _contentView.layer.cornerRadius =5;
    [self addSubview:_contentView];
    
    _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 10, ScreenWidth-20, 200)];
    //语言
    [_datePicker setLocale:[NSLocale localeWithLocaleIdentifier:@"Chinese"]];
    //最大值最小值
    [_datePicker setMaximumDate:[NSDate date]];
    [_datePicker setMinimumDate:[NSDate dateWithTimeIntervalSinceNow:-5*365*24*60*60]];
    //日期的表示格式
    [_datePicker setDatePickerMode:UIDatePickerModeDate];
    
    [_contentView addSubview:_datePicker];
    
    //确认按钮
    self.okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.okBtn.frame= CGRectMake(0, CGRectGetMaxY(_datePicker.frame), ScreenWidth-19.2, 40);
    [self.okBtn setTitle:@"确认" forState:UIControlStateNormal];
    [self.okBtn addTarget:self action:@selector(OKAction:) forControlEvents:UIControlEventTouchUpInside];
    self.okBtn.backgroundColor =[UIColor colorWithWhite:0.9 alpha:1.0];
    [self.okBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_contentView addSubview:self.okBtn];
    //取消按钮
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelBtn.frame= CGRectMake(0, CGRectGetMaxY(_okBtn.frame)+1, ScreenWidth-20, 40);
    self.cancelBtn.layer.cornerRadius =5;
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    self.cancelBtn.backgroundColor =[UIColor colorWithWhite:0.9 alpha:1.0];
    [self.cancelBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_contentView addSubview:self.cancelBtn];
    
}
-(void)clickEmpty:(UIGestureRecognizer *)sender
{
    [self removeFromSuperview];
}
//传值
-(void)OKAction:(UIButton *)sender
{
    if (_mySelectDate) {
        _mySelectDate([self.formatter stringFromDate:_datePicker.date]);
    }
    [self removeFromSuperview];
}
-(void)cancelAction:(UIButton *)sender
{
    [self removeFromSuperview];
}
- (NSDateFormatter *)formatter {
    if (_formatter) {
        return _formatter;
    }
    _formatter =[[NSDateFormatter alloc] init];
    [_formatter setDateFormat:@"yyyy-MM-dd"];
    return _formatter;
    
}

@end
